package com.github.xwc.example;

import com.github.xwc.example.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 主页
 *
 * @author wuhenzhizao
 * @since 2017-08-02
 */
public class MainAbility extends Ability {
    private static final int TWO = 2;
    private static final int PERMISSION_REQUEST_CODE = 0;
    private static final String INTERNET = "ohos.permission.INTERNET";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        requestPermission();
    }

    private void requestPermission() {
        if (verifyCallingPermission(INTERNET) != IBundleManager.PERMISSION_GRANTED) {
            if ( canRequestPermission(INTERNET)) {
                requestPermissionsFromUser(new String[]{INTERNET}, PERMISSION_REQUEST_CODE);
            }
        }
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && (grantResults[0] == IBundleManager.PERMISSION_GRANTED
                    && grantResults[1] == IBundleManager.PERMISSION_GRANTED
                    && grantResults[TWO] == IBundleManager.PERMISSION_GRANTED)) {
                } else {
                }
                return;
            default:
                break;
        }
    }
}
